package cn.rtangyy.seckilling.service.impl;

import cn.rtangyy.seckilling.dao.OrderDao;
import cn.rtangyy.seckilling.entity.Order;
import cn.rtangyy.seckilling.entity.SeckillGoods;
import cn.rtangyy.seckilling.entity.SeckillOrder;
import cn.rtangyy.seckilling.entity.User;
import cn.rtangyy.seckilling.exception.GlobalException;
import cn.rtangyy.seckilling.service.GoodsService;
import cn.rtangyy.seckilling.service.OrderService;
import cn.rtangyy.seckilling.service.SeckillGoodsService;
import cn.rtangyy.seckilling.service.SeckillOrderService;
import cn.rtangyy.seckilling.utils.MD5Util;
import cn.rtangyy.seckilling.utils.UUIDUtil;
import cn.rtangyy.seckilling.vo.GoodsVo;
import cn.rtangyy.seckilling.vo.OrderDetailVo;
import cn.rtangyy.seckilling.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private SeckillGoodsService seckillGoodsService;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private SeckillOrderService seckillOrderService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Transactional
    @Override
    public Order seckill(User user, GoodsVo goodsVo) {
        // 秒杀商品减库存
        SeckillGoods seckillGoods = seckillGoodsService.findByGoodsId(goodsVo.getId());

//        seckillGoodsService.updateById(seckillGoods);
        // 不能直接进行更新库存操作，而必须保证在库存不为零的情况下进行操作
        boolean result = seckillGoodsService.updateByIdPositiveStock(seckillGoods);
        if (seckillGoods.getStockCount() < 1) {
            // 设置库存为0
            redisTemplate.opsForValue().set("isStockEmpty:" + goodsVo.getId(), "0");
            return null;
        }

        // 生成订单
        Order order = new Order();
        order.setGoodsId(goodsVo.getId());
        order.setUserId(user.getId());
        order.setDeliverAddrId(0L);
        order.setGoodsName(goodsVo.getGoodsName());
        order.setGoodsCount(1);
        order.setGoodsPrice(seckillGoods.getSeckillPrice());
        order.setOrderChannel(1);
        order.setStatus(0);
        order.setCreateDate(new Date());
        orderDao.insertOrder(order);


        // 生成秒杀订单
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setUserId(user.getId());
        seckillOrder.setOrderId(order.getId());
        seckillOrder.setGoodsId(goodsVo.getId());
        seckillOrderService.insertSecOrder(seckillOrder);

        // 使用 redis 存储秒杀订单
        redisTemplate.opsForValue().set("order:" + user.getId() + ":" + goodsVo.getId(), seckillOrder);
        return order;
    }

    @Override
    public String createPath(User user, Long goodsId) {
        String str = MD5Util.md5(UUIDUtil.uuid() + "123456");
        redisTemplate.opsForValue().set("seckillPath:" + user.getId() + ":" + goodsId, str, 60L, TimeUnit.SECONDS);
        return str;
    }

    @Override
    public OrderDetailVo detail(Long orderId) {
        if (orderId == null) {
            throw new GlobalException(RespBeanEnum.ORDER_NOT_EXIST);
        }
        Order order = orderDao.findOrderById(orderId);
        GoodsVo goodsVo = goodsService.findGoodsVoById(order.getGoodsId());
        OrderDetailVo orderDetailVo = new OrderDetailVo(order, goodsVo);
        return orderDetailVo;
    }

    @Override
    public boolean checkPath(User user, Long goodsId, String path) {
        if (user == null || StringUtils.isEmpty(path)) {
            return false;
        }
        String redisPath = (String) redisTemplate.opsForValue().get("seckillPath:" + user.getId() + ":" + goodsId);
        return path.equals(redisPath);
    }

    @Override
    public boolean checkCaptcha(User user, Long goodsId, String captcha) {
        if (user == null || StringUtils.isEmpty(captcha) || goodsId < 0) {
            return false;
        }
        String redisCaptcha = (String) redisTemplate.opsForValue().get("captcha:" + user.getId() + ":" + goodsId);
        return captcha.equals(redisCaptcha);
    }
}
